package com.ztz.mynewdemo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.liaoinstan.springview.container.AliFooter;
import com.liaoinstan.springview.container.AliHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.ztz.mynewdemo.R;
import com.ztz.mynewdemo.adapter.TodayAdapter;
import com.ztz.mynewdemo.bean.TodayResponse;
import com.ztz.mynewdemo.utils.NewsApi;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by wqewqe on 2017/5/18.
 */

public class TodayHistoryFrag extends Fragment {
    RecyclerView todayRecycler;
    SpringView todaySpring;
    List<TodayResponse.ResultBean> resultBeanList=new ArrayList<>();
    TodayAdapter adapter;
    FloatingActionButton top_iv;
    Calendar calendar;
    int year,month,day;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.today_fragment,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        todayRecycler=(RecyclerView)view.findViewById(R.id.today_recycler);
        todaySpring=(SpringView)view.findViewById(R.id.today_spring);
//        todaySpring.setListener(new SpringView.OnFreshListener() {
//            @Override
//            public void onRefresh() {
////                getData(month,day);
//            }
//
//            @Override
//            public void onLoadmore() {
//                getData(month,day);
//            }
//        });
        top_iv=(FloatingActionButton) view.findViewById(R.id.top);
        top_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                todayRecycler.smoothScrollToPosition(0);
            }
        });
        calendar=Calendar.getInstance();
        year=calendar.get(Calendar.YEAR);
        month=calendar.get(Calendar.MONTH)+1;
        day=calendar.get(Calendar.DAY_OF_MONTH);

        todaySpring.setHeader(new AliHeader(getContext()));
        todaySpring.setFooter(new AliFooter(getContext()));
        LinearLayoutManager manager=new LinearLayoutManager(getContext());
        todayRecycler.setLayoutManager(manager);
        adapter=new TodayAdapter(getContext(),resultBeanList);
        todayRecycler.setAdapter(adapter);

        getData(month,day);
    }

    public void getData(int month,int day) {
        OkGo.<String>get(NewsApi.TODAY)
                .tag(this)
                .params("key","907298e415a8318e78b1cb4a3403c01e")
                .params("v",1.0)
                .params("month",month)
                .params("day",day)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String s = response.body();
                        Gson gson=new Gson();
                        TodayResponse todayResponse=gson.fromJson(s,TodayResponse.class);
                        resultBeanList.clear();
                        resultBeanList.addAll(todayResponse.getResult());
                       getActivity().runOnUiThread(new Runnable() {
                           @Override
                           public void run() {
                               adapter.notifyDataSetChanged();
                           }
                       });
                    }
                });

    }
}
